package kt.com.bilin.coin.ui.dialog

import android.app.Dialog
import android.content.Context
import android.text.InputType
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
import com.bilin.coin.R
import kotlinx.android.synthetic.main.dialog_input.*
import kt.com.bilin.coin.common.getString
import kt.com.bilin.coin.utils.BLUtils

/**
 * 输入页面
 */
class InputDialog(
        context: Context?,
        title: String, hint: String,
        settingText: ((editText: EditText) -> Unit)? = null,
        private var linsnter: ((String) -> Unit?)? = null,
) : Dialog(context!!, R.style.dimLoadingdialog) {
    companion object {
        fun showPayPassword(context: Context?, linsnter: ((String) -> Unit?)? = null) {
            InputDialog(context, R.string.dialog_pay_password.getString(), R.string.dialog_pay_password_hint.getString(), {
                it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }, linsnter).show()
        }
    }

    init {
        setContentView(R.layout.dialog_input)
        settingText?.invoke(etContent)
        tvTitle.text = title
        etContent.hint = hint
        buttonConfirm.setOnClickListener {
            linsnter?.let {
                if (etContent.text.toString().trim().isNotEmpty()) {
                    it(etContent.text.toString())
                    BLUtils.hideSoftInput(etContent)
                    dismiss()
                }
            }
        }
        window?.let {
            val attributes = it.attributes
            attributes.width = (getContext().resources.displayMetrics.widthPixels * 0.92).toInt()
            attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
            it.attributes = attributes
            it.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        }
    }

    override fun show() {
        super.show()
        if (etContent != null) {
            etContent.setText("")
            etContent.isFocusable = true
            etContent.isFocusableInTouchMode = true
            etContent.requestFocus()
        }
    }
}